package top.yihoxu.yoj.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Author dth
 * @Date 2024/5/7 22:09
 * @PackageName:top.yihoxu.yoj.config
 * @ClassName: RedisConfig
 * @Description: TODO
 * @Version 1.0
 */
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        // 设置键序列化方式
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());

        // 设置值序列化方式，根据需要配置，例如使用Jackson2JsonRedisSerializer
         template.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
         template.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));

        return template;
    }
}
